package com.lapula.crazyface;

import java.io.OutputStream;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.AndroidRuntimeException;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.ImageView.ScaleType;

import com.lapula.crazyface.gif.GifGenerator;
import com.lapula.crazyface.util.FileUtil;
import com.lapula.crazyface.util.ImageUtils;

public class FacePreviewActivity extends Activity {
	private ImageView imageView;
	private Button okBtn;
	private Button cancelBtn;
	private PreviewPopupWindow popupWindow;

	private int index = 0;
	private List<Bitmap> pics;
	private int picsNum = 0;
	TimerTask task;

	Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			try {
				super.handleMessage(msg);
				Bitmap bitmap = (Bitmap) msg.obj;
				
				DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
				int displayWidth = displayMetrics.widthPixels;
				int displayHeight = displayMetrics.heightPixels - 50;
				Bitmap newBitmap = ImageUtils.scaleBitmap(bitmap, displayWidth, displayHeight);
				imageView.setImageBitmap(newBitmap);
			} catch (AndroidRuntimeException runtimeException) {
				runtimeException.printStackTrace();
			}
		}

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_face_preview);
		
		findViewById(R.id.iv_cancel).setOnClickListener(cancelListener);
		findViewById(R.id.btn_confirm).setOnClickListener(confirmListener);

		imageView = (ImageView) findViewById(R.id.imageView);

		pics = D.I().getPreviewPics();
		picsNum = pics.size();

		task = new TimerTask() {
			public void run() {
				Message msg = new Message();
				msg.obj = pics.get(index);
				handler.sendMessage(msg);
				index = (index + 1) % picsNum;
			}
		};
		Timer timer = new Timer();
		timer.schedule(task, 0, 1000 / Constants.AVERAGE_PREVIEW_FRAME_RATE);
		
		popupWindow = new PreviewPopupWindow(this);
	}

	@Override
	protected void onDestroy() {
		task.cancel();
		imageView.setImageBitmap(null);
		D.I().clearPreviewPics();
		super.onDestroy();
	}

	private OnClickListener cancelListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			popupWindow.showAtLocation(FacePreviewActivity.this.findViewById(R.id.activity_face_preview), Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0); 
		}
	};
	
	private OnClickListener confirmListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			OutputStream os = FileUtil.getFileOutputStream(FacePreviewActivity.this, String.valueOf(System.currentTimeMillis()) + ".gif");
			boolean flag = GifGenerator.bitmap2Gif(D.I().getPreviewPics(), os, 1000 / Constants.AVERAGE_PREVIEW_FRAME_RATE);
			if (flag) {
				Toast.makeText(FacePreviewActivity.this, "Create Successfully", Toast.LENGTH_SHORT).show();
			} else {
				Toast.makeText(FacePreviewActivity.this, "Create Failed", Toast.LENGTH_SHORT).show();
			}
			finish();
		}
	};
}
